package com.lottery.im.module.setting.presenter;

import android.app.Activity;
import com.lottery.im.MyApplication;
import com.lottery.im.api.ApiClient;
import com.lottery.im.api.observer.BaseListObserver;
import com.lottery.im.api.result.entity.OtherUserInfo;
import com.lottery.im.base.BaseFragmentPresenter;
import com.lottery.im.module.setting.view.ProfileView;
import com.trello.rxlifecycle2.LifecycleProvider;
import com.trello.rxlifecycle2.android.FragmentEvent;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public class ProfilePresenter extends BaseFragmentPresenter<ProfileView> {

  public ProfilePresenter(Activity activity,
      ProfileView iView,
      LifecycleProvider<FragmentEvent> provider) {
    super(activity, iView, provider);
  }

  /**
   * 通过第三方ID获取自己的VIP等级
   */
  public void getSelfLevel() {

    ApiClient.getApiRetrofitInstance()
        .getUserByThirdIds(MyApplication.getInstance().getThirdAccId())
        .throttleFirst(2, TimeUnit.SECONDS)
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .compose(provider.bindToLifecycle())
        .subscribe(new BaseListObserver<Map<String, OtherUserInfo>>() {
          @Override public void onStart() {
          }

          @Override public void onSuccess(List<Map<String, OtherUserInfo>> t) {

            if (null != t && t.size() > 0) {
              try {
                iView.getSelfLevelSuc(
                    t.get(0).get(MyApplication.getInstance().getThirdAccId()).getUser_level());
              } catch (Exception e) {

              }
            }
          }

          @Override public void onFail(String code, String msg) {
          }

          @Override public void onException(String msg) {
          }

          @Override public void onFinish() {

          }
        });
  }
}
